﻿using msilgc.cbuilder.CSyntaxTree;
using msilgc.cbuilder.MSILSyntaxTree;
using msilgc.CSyntaxTree;

namespace msilgc.cbuilder.CBuilder
{
    internal class ArrayLengthTranslator : ITranslator {
        private readonly VariableFactory _varFactory;

        public ArrayLengthTranslator(VariableFactory varFactory) {
            _varFactory = varFactory;
        }

        public ICStatement TranslateAndAdd(CMethodBody methodBody, CBasicBlock currentBlock, BasicBlock block, Instruction instruction) {
            IVariable arrayVar = EvaluationStackHelper.PopVariableOffEvaluationStack(currentBlock);
            ArrayLengthStatement arrayLenStm = new ArrayLengthStatement(_varFactory.CreateNext(typeof(int)), arrayVar);
            currentBlock.Statements.Add(arrayLenStm);
            return arrayLenStm;
        }
    }
}